import { API_BASE_URL } from '@/config/setting';
export const formatDate = (date, format = 'yyyy-MM-dd hh:mm:ss') => {
  var o = {
    // 月
    'M+': date.getMonth() + 1,
    // 天
    'd+': date.getDate(),
    // 小时
    'h+': date.getHours(),
    // 分钟
    'm+': date.getMinutes(),
    // 秒
    's+': date.getSeconds(),
    // 季度
    'q+': Math.floor((date.getMonth() + 3) / 3),
    // 毫秒
    S: date.getMilliseconds()
  };
  if (/(y+)/.test(format))
    format = format.replace(
      RegExp.$1,
      (date.getFullYear() + '').substr(4 - RegExp.$1.length)
    );
  for (let k in o)
    if (new RegExp('(' + k + ')').test(format))
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      );
  return format;
};
 /**
     * 文件转Base64
     * @param file
     */
 export const fileToBase64=(file)=> {
    if (!file) return null;
    return new Promise(resolve => {
        let reader = new FileReader();
        reader.onload = (ev) => resolve(ev.target.result);
        reader.onerror = () => resolve(null);
        reader.readAsDataURL(file);
    });
}

// type=1 app  type=2 广告
export const uploadToImg = async (img,type) => {
  try {
    // let baseURL='http://192.168.1.32:8082/admin'
    const response = await fetch(API_BASE_URL + "/Img?img=" + img+"&type=" + type, {
      headers: {
        Authorization: `${localStorage.getItem("token")}`,
      },
    });
    if (response.ok) {
      const blob = await response.blob();
      const objectUrl = URL.createObjectURL(blob);
      // this.dialog.data.img = objectUrl;
      return objectUrl;
      //   console.log("获取图片:", objectUrl);
    }
  } catch (error) {
    console.error("获取图片时出错:", error);
  }
};
